//
//  XXJWebViewController.m
//  gosh
//
//  Created by 许小军 on 16/9/24.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "XXJWebViewController.h"
#import <WebKit/WebKit.h>
@interface XXJWebViewController ()
@property (weak, nonatomic) IBOutlet UIView *containtView;
@property(strong,nonatomic)WKWebView * webView;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *goBack;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goForward;

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;



@end

@implementation XXJWebViewController
- (IBAction)goBack:(id)sender {
    [self.webView goBack];
}
- (IBAction)gofward:(id)sender {
    [self.webView goForward];
}
- (IBAction)refresh:(id)sender {
    [self.webView reload];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _webView=[[WKWebView alloc]init];
    [self.containtView addSubview:_webView];
    
    NSURLRequest * request=[NSURLRequest requestWithURL:_url];
    [_webView loadRequest:request];
    
    //kvo监听前进 后退  刷新
    [_webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
    
    [_webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
    
    
    
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    self.goBack.enabled=self.webView.canGoBack;
    self.goForward.enabled=self.webView.canGoForward;
    
    self.progressView.progress=self.webView.estimatedProgress;
    self.progressView.hidden=self.webView.estimatedProgress>=1;
}

-(void)dealloc
{
    [self.webView removeObserver:self forKeyPath:@"canGoBack"];
    [self.webView removeObserver:self forKeyPath:@"canGoForward"];
    
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    _webView.frame=self.containtView.bounds;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
